package com.egoo.ticket.server.utils.command;

import com.egoo.ticket.server.service.EurmDataSyncService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: victor_tang
 * @Date: 2020/7/9 17:49
 * @Description: 系统启动自动调用接口
 */
@Component
@Slf4j
@Order(value = 100)
public class MyCommandLineRunner implements CommandLineRunner {
	@Autowired
	private EurmDataSyncService eurmDataSyncService;
	@Value("${eurmSynData.syn-tenantIds}")
	private String synTenantIds;
	@Value("${eurmSynData.restart-syn-switch}")
	private Boolean restartSynSwitch;
	@Override
	public void run(String... args){
		//全量获取内管租户（因内管租户过多，暂注释，读取配置文件）
		//List<String> tenants = eurmDataSyncService.getTenant();
		if (StringUtils.isEmpty(synTenantIds)) {
			log.error(synTenantIds+"需要同步用户，角色，部门信息的租户id不能为空！");
			return;
		}
		if (!restartSynSwitch) {
			return;
		}
		List<String> tenantIdList = Arrays.asList(synTenantIds.split(","));

		tenantIdList.forEach(v->{
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.USER);
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.ROLE);
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.DEPT_GROUP);
		});
		// 给各租户初始化数据
		List<String> arrayList = new ArrayList<>(tenantIdList);
		eurmDataSyncService.initDBData(arrayList);
	}
}
